package com.base.controller;

import com.base.entity.OrderEntity;
import com.base.service.OrderService;
import com.base.util.RedisTool;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@RestController
public class MyController {

    @Autowired
    private RedisTool redisTool;

    @Autowired
    private OrderService orderService;

    BloomFilter<String> integerBloomFilter = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), 10000, 0.01);;

    @RequestMapping("/set")
    public String set(){
        redisTool.set("20200516","good day");
        return ("20200516 is " + redisTool.get("20200516"));
    }

    @RequestMapping("/save")
    public String save() {
        String uuid = UUID.randomUUID().toString();
        String orderId = "orderid"+System.currentTimeMillis();
        OrderEntity orderEntity = new OrderEntity("orderid"+System.currentTimeMillis(), "ordername");
        orderEntity.setOrderState(0);
        orderEntity.setCommodityId(System.currentTimeMillis());
        orderEntity.setOrderCreatetime(new Date());
        orderEntity.setOrderMoney(100.0);
        orderEntity.setOrderToken(uuid);
        orderService.save(orderEntity);

        orderService.save(orderEntity);

        redisTool.set(orderId,uuid);
        redisTool.expire(orderId,5);

        // 添加到我们的布隆过滤器中
        integerBloomFilter.put(orderEntity.getOrderId());

        return orderEntity.toString();
    }

    //要使用布隆过滤器，要先进行预热，将DB的数据刷到 布隆过滤器中。
    @RequestMapping("/dbToBulong")
    public String dbToBulong() {
        // 1.从数据库预热id到布隆过滤器中
        List<OrderEntity> orderEntitys = orderService.findAll();
        // integerBloomFilter = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), orderEntitys.size(), 0.01);
        for (int i = 0; i < orderEntitys.size(); i++) {
            // 添加到我们的布隆过滤器中
            integerBloomFilter.put(orderEntitys.get(i).getOrderId());
        }

        return "success";
    }

    @RequestMapping("/find/{orderId}")
    public String find(@PathVariable(value = "orderId") String orderid) {

        // 0.判断我们的布隆过滤器
        if (!integerBloomFilter.mightContain(orderid)) {
            System.out.println("从布隆过滤器中查询不存在");
            return "从布隆过滤器中查询不存在";
        }

        OrderEntity orderEntity = orderService.findByOrderId(orderid);
        return orderEntity.toString();
    }

    @RequestMapping("/find1/{orderId}")
    public String find1(@PathVariable(value = "orderId") String orderid) {
        OrderEntity orderEntity = orderService.findByOrderId1(orderid);
        return orderEntity.toString();
    }
}
